import request from "@/utils/request";

const baseUrl = "systemRole/";

/**
 * 获取所有菜单
 * @param pagination
 * @param keyword
 * @returns {AxiosInstance}
 */
export function page(pagination, keyword) {
    return new request({
        url: `${baseUrl}list`,
        method: "GET",
        params: {...pagination, ...keyword}
    })
}

/**
 * 添加单个角色
 * @returns {AxiosInstance}
 * @param data
 */
export function addRole(data) {
    return new request({
        url: `${baseUrl}`,
        method: "POST",
        data: data
    })
}

/**
 * 修改单个角色
 * @returns {AxiosInstance}
 * @param data
 */
export function updateRole(data) {
    return new request({
        url: `${baseUrl}`,
        method: "PUT",
        data: data
    })
}

/**
 * 获取所有角色
 * @returns {AxiosInstance}
 */
export function getAllRoles() {
    return new request({
        url: `${baseUrl}all`,
        method: "GET"
    })
}

/**
 * 获取角色的菜单列表
 */
export function getRoleMenu(roleId) {
    return new request({
        url: `${baseUrl}menu`,
        method: "GET",
        params: {roleId}
    })
}

/**
 * 分配权限，根据角色id
 * @param roleId
 * @param menuIds
 * @returns {AxiosInstance}
 */
export function updateMenu(roleId, menuIds) {
    return new request({
        url: `${baseUrl}update/menu`,
        method: "PUT",
        data: {roleId, menuIds}
    })
}

/**
 * 是否启用角色
 */
export function enableRole({id, status}) {
    return new request({
        url: `${baseUrl}`,
        method: "PUT",
        data: {id, status}
    })
}


/**
 * 删除单个角色
 * @returns {AxiosInstance}
 * @param id
 */
export function deleteRole(id) {
    return new request({
        url: `${baseUrl}?id=${id}`,
        method: "DELETE",
    })
}

/**
 * 删除多个角色
 * @returns {AxiosInstance}
 * @param ids
 */
export function deleteMultipleRole(ids) {
    let filterIds = "?";
    ids.forEach(item => filterIds += `ids=${item}&`);
    filterIds = filterIds.substr(0, filterIds.length - 1);
    return new request({
        url: `${baseUrl}multiple${filterIds}`,
        method: "DELETE",
    })
}